
// pages/collection/collection.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  onEnterDetail: function (event) {
    var id = event.currentTarget.dataset.id;
    my.navigateTo({
      url: '/pages/detail/detail?id=' + id
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var list = my.getStorageSync({key: 'collection'})['data'];

    if (list && list.length > 0) {
      that.setData({
        'list': list,
        'num': list.length
      });
    } else {
      that.setData({
        'num': 0
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //wx.startPullDownRefresh()
    this.refreshData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshData();
  },

  /**
   * 下拉刷新更新数据
   */
  refreshData: function () {
    my.showNavigationBarLoading(); //在标题栏中显示加载

    var that = this;
    var collection = my.getStorageSync({key: 'collection'})['data'];

    if (!collection) {
      collection = new Array();
      my.setStorageSync({key: "collection", data: collection});
    } //模拟加载


    setTimeout(function () {
      my.hideNavigationBarLoading(); //完成停止加载

      my.stopPullDownRefresh(); //停止下拉刷新

      that.setData({
        'list': collection,
        'num': collection.length
      });

      if (collection.length == 0) {
        my.showToast({
          content: '无收藏菜谱'
        });
      }
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  enterNcov: function(){
    my.navigateToMiniProgram({
      appId: '2021001132600368',
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});